package com.xsh.controller;

import com.github.pagehelper.PageInfo;
import com.xsh.pojo.Goods;
import com.xsh.pojo.GoodsType;
import com.xsh.service.GoodsService;
import com.xsh.service.GoodsTypeService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/goods")
@RequiresAuthentication
public class GoodsController {

    private ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();

    private GoodsService goodsService = context.getBean("goodsServiceImpl", GoodsService.class);

    private GoodsTypeService goodsTypeService = context.getBean("goodsTypeServiceImpl", GoodsTypeService.class);


    @GetMapping("/list")
    public String list(Integer pageNum, HttpServletRequest request){
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<Goods> pageInfo = goodsService.queryAll(pageNum);
        request.setAttribute("pageInfo", pageInfo);
        return "WEB-INF/goodsList";
    }

    @GetMapping("/addPage")
    public String addPage(HttpServletRequest request){
        List<String> goodsTypes = goodsTypeService.queryAll();
        System.out.println(1);
        request.setAttribute("goodsTypes", goodsTypes);
        return "WEB-INF/addGoods";
    }

    @PostMapping("/add")
    public String add(Goods goods, MultipartFile picture, HttpSession session) throws IOException {
        String realPath = session.getServletContext().getRealPath("");
        String path = "images" + File.separator + "goods" + File.separator + picture.getOriginalFilename();
        picture.transferTo(new File(realPath + path));
        goods.setPictureAddress(path);
        goodsService.addGoods(goods);
        return "redirect:/goods/list";
    }

    @PostMapping("/addPicture")
    @ResponseBody
    public void addPicture(MultipartFile picture){
        //goodsService.addGoods(goods);
        System.out.println(picture.getOriginalFilename());
    }

    @GetMapping("/updatePage")
    public String updatePage(int id, HttpServletRequest request){
        Goods goods = goodsService.queryByGoodsId(id);
        List<String> goodsTypes = goodsTypeService.queryAll();
        goodsTypes.remove(goods.getTypeName());
        request.setAttribute("goods", goods);
        request.setAttribute("goodsTypes", goodsTypes);
        return "WEB-INF/updateGoods";
    }

    @PutMapping("/update")
    @ResponseBody
    public void update(@RequestBody Goods goods){
        GoodsType goodsType = goodsTypeService.queryByTypeName(goods.getTypeName());
        goods.setTypeId(goodsType.getId());
        goodsService.updateGoods(goods);
    }

    @GetMapping("/delete")
    public String deletePage(int id){
        goodsService.deleteGoods(id);
        return "redirect:/goods/list";
    }
}
